<template>
    <div class="container">
        <h3>仓储管理</h3>
		<el-row style="margin-top:20px;">
			<el-col :span="7">
				<el-input placeholder="请输入关键词" v-model="searchName" style="width:300px;">
					<i slot="prefix" class="el-input__icon el-icon-search" ></i>
					<el-button slot="append" @click="handleSearch">搜索</el-button>
				</el-input>
			</el-col>
		   
			<el-button type="primary" v-if="!warehousing" @click="goaddprocess" style="float:right; margin-right: 10px">入库</el-button>
		</el-row>
			
		<el-table
			:data="tableData"
			border
			style="margin-top:20px;"
			ref="multipleTable"
			header-cell-class-name="table-header"
		>
			<el-table-column prop="productName" sortable label="产品名称"></el-table-column>
			<el-table-column prop="warehouseName" sortable label="仓位名称"></el-table-column>
			<el-table-column prop="specs" sortable label="规格">
				<template slot-scope="scope">
					{{scope.row.specs}}{{scope.row.unit}}
				</template>
			</el-table-column>
			<el-table-column prop="subtotal" sortable label="数量">
				<temmplate slot-scope="scope">
					{{scope.row.subtotal}}{{scope.row.units}}
				</temmplate>
				
			</el-table-column>
			<el-table-column prop="storageBatch" sortable label="批号"></el-table-column>
			<el-table-column prop="source" sortable label="入库来源"></el-table-column>
			<el-table-column label="操作" width="250" align="center">
				<template slot-scope="scope">
				   <el-button
				   		v-if="!toBeDelivered"
					   type="text"
					   @click="handlePending(scope.$index, scope.row)"
				   >待出库</el-button>
				   <!-- v-if="scope.row.isOut == 0" -->
				   <el-button
				   		v-if="!curing"
					   type="text"
					   @click="handleConserve(scope.$index, scope.row)"
				   >养护</el-button>
				   <el-button
				   		v-if="!adjustInventory"
					   type="text"
					   @click="handleEdit(scope.$index, scope.row)"
				   >调整库存</el-button>
					<el-button
						v-if="!detailed"
						type="text"
						@click="handleCheck(scope.$index, scope.row)"
					>明细</el-button>
				</template>
			</el-table-column>
		</el-table>
		<el-pagination layout="total, prev, pager, next, jumper" :page-size="pagesize" :current-page.sync="currentPage" :total="total"  @current-change="handleCurrentChange" style="margin-top:20px"></el-pagination>
		<el-dialog :title="drugName" customClass="customWidth" :visible.sync="dialogFormVisible">
			<el-form :model="form" label-width="120px">
				<el-form-item label="账面库存">
					<el-input placeholder="请输入内容" disabled  v-model="num" style="width:200px">
					</el-input>
					
					<span style="margin-left:20px;">{{form.units}}</span>
				</el-form-item>
				<el-form-item label="实际库存">
					<el-input placeholder="请输入内容"   v-model="form.actualNum" style="width:200px">
					</el-input>
					<span style="margin-left:20px;">{{form.units}}</span>
				</el-form-item>
				<el-form-item label="备注">
					<textarea name="" id="" cols="30" rows="10" v-model="form.remarks"></textarea>
				</el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="close">取 消</el-button>
				<el-button type="primary" @click="save">确 定</el-button>
			</div>
		</el-dialog>
    </div>
	
</template>

<script>
	import { storage_findByList,storage_adjustment } from "@/request/api"
    export default {
        data() {
            return {
				units:'',
				num:'',
				drugName:'',
				dialogFormVisible:false,
                total:0,
                currentPage:1,
                pagesize:10,
                searchName: '',
                tableData: [],
                form: {},
				source:'',
				productName:'',
				warehouseId:'',
                action:'',
				specs:'',
				warehousing:true,
				adjustInventory:true,
				detailed:true,
				toBeDelivered:true,
				curing:true,
            };
        },
        created(){
            
        },
        mounted(){
			this.init();
			this.judgmentAuthority();
        },
        methods: {
			save(){
				// console.log(this.form)
				this.form.actualNum = this.form.actualNum/this.specs
				storage_adjustment(this.form).then((res)=>{
					this.$notify({
					    title: '提示',
					    message: '调整成功',
					    type: 'success',
					    duration:1000
					});
					this.init()
					this.dialogFormVisible = false
				})
			},
			close(){
				this.dialogFormVisible = false
			},
			handlePending(index,row){
				this.$router.push({
					path:'/warehouseManagement/forOutBound',
					query:{id:row.id}
				})
			},
			handleConserve(index,row){
				this.$router.push({
					path:"/warehouseManagement/conserve",
					query:{id:row.id}
				})
			},
			handleEdit(index,row){
				
				var r = row.unit
				var site  =  r.lastIndexOf("\/")
				row.numunits = r.substring(site + 1)
				var st = r.substr(r.lastIndexOf('/', r.lastIndexOf('/') - 1) + 1);
				var str = st.lastIndexOf("\/")
				this.form.units = st.substring(0,str)
				this.drugName = row.productName
				this.num = row.subtotal
				this.units = row.units
				this.form.id = row.id 
				this.form.storageId = row.id
				this.dialogFormVisible = true
				this.specs = row.specs
			},
            goaddprocess(){
                this.$router.push({
                    path:'/warehouseManagement/putInStorage'
                })
            },
            handleCurrentChange(val){
                this.currentPage = val
                this.init()
            },
            init(){
				let data = {
					warehouseId:this.warehouseId,
					productName:this.productName,
					source:this.source,
					page:this.currentPage
				}
				storage_findByList(data).then((res)=>{
					this.tableData = res.rows
					this.total = res.total
					for(var i=0;i<this.tableData.length;i++){
						var st = this.tableData[i].unit.substr(this.tableData[i].unit.lastIndexOf('/', this.tableData[i].unit.lastIndexOf('/') - 1) + 1);
						var str = st.lastIndexOf("\/")
						this.tableData[i].units = st.substring(0,str)
					}
				})
            },
			handleCheck(index,row){
				this.$router.push({
					path:'/warehouseManagement/check',
					query:{id:row.id}
				})
			},
            //搜索
            handleSearch(){
                
			},
			//判定操作按钮显示
			judgmentAuthority() {
				var type = JSON.parse(localStorage.getItem('userInfo')).appMenus;
				type.map((item) => {
					if (item.appName == '种植平台') {
						item.menus.map((item1) => {
							if (item1.name == '仓储管理' && item1.children != null) {
								for (var i = 0; i < item1.children.length; i++) {
									if (item1.children[i].name == '入库') this.warehousing = false;
									if (item1.children[i].name == '调整库存') this.adjustInventory = false;
									if (item1.children[i].name == '明细') this.detailed = false;
									if (item1.children[i].name == '待出库') this.toBeDelivered = false;
									if (item1.children[i].name == '养护') this.curing = false;
								}
							}
						});
					}
				});
			} 
           
        }
    }
</script>

<style  lang="less">

.el-pagination{
    text-align: center;
}

/deep/.el-dialog{
	  display: flex;
	  flex-direction: column;
	  margin:0 !important;
	  position:absolute;
	  top:50%;
	  left:50%;
	  transform:translate(-50%,-50%);
	  max-height:calc(100% - 30px);
	  max-width:calc(100% - 30px);
	}
	.customWidth{
		width:30%;
	}
</style>